package xjh.him.activity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xjh.him.R;

import java.util.HashMap;

import xjh.him.common.BgTaskActivity;
import xjh.him.common.SharedData;
import xjh.him.entity.ChatMsg;
import xjh.him.fragment.HomeContentFragment;
import xjh.him.netty.codec.Packet;
import xjh.him.netty.dto.FriendDto;


public class HomeActivity extends BgTaskActivity implements Handler.Callback {
    private FragmentManager fm;
    private static final HashMap<Integer, HomeContentFragment> fragMap = new HashMap<>();
    public Handler handler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        handler = new Handler(this);
        SharedData.updateMsgItemHandler = handler;

        prepareLoadData();
        showFragment(R.id.tab_msg);
    }

    public void prepareLoadData() {
        // load friendList
        if (SharedData.friendBeans == null) {
            FriendDto.SelectFriendsReq req = new FriendDto.SelectFriendsReq();
            req.setUserId(SharedData.me.getId());
            SharedData.client.sendMsg(Packet.create(FriendDto.SelectFriendsReq.class, req));
            Object obj = SharedData.future.get();
            if (obj instanceof FriendDto.SelectFriendsResp) {
                FriendDto.SelectFriendsResp resp = (FriendDto.SelectFriendsResp) obj;
                SharedData.friendBeans = resp.getUserList();
            }
        }
    }

    public void showFragment(int resId) {
        fm = getFragmentManager();
        FragmentTransaction trx = fm.beginTransaction();
        hideAllFragment(trx);

        switch (resId) {
            case R.id.tab_msg:
                HomeContentFragment frag = fragMap.get(R.id.tab_msg);
                if (frag == null) {
                    frag = new HomeContentFragment();
                    frag.setResId(R.id.tab_msg);
                    trx.add(R.id.home_content, frag);
                    fragMap.put(R.id.tab_msg, frag);
                }else {
                    trx.show(frag);
                }
                break;
            case R.id.tab_friend:
                frag = fragMap.get(R.id.tab_friend);
                if (frag == null) {
                    frag = new HomeContentFragment();
                    frag.setResId(R.id.tab_friend);
                    trx.add(R.id.home_content, frag);
                    fragMap.put(R.id.tab_friend, frag);
                }else {
                    trx.show(frag);
                }
                break;
            case R.id.tab_discover:
                frag = fragMap.get(R.id.tab_discover);
                if (frag == null) {
                    frag = new HomeContentFragment();
                    frag.setResId(R.id.tab_discover);
                    trx.add(R.id.home_content, frag);
                    fragMap.put(R.id.tab_discover, frag);
                }else {
                    trx.show(frag);
                }
                break;
            case R.id.tab_me:
                frag = fragMap.get(R.id.tab_me);
                if (frag == null) {
                    frag = new HomeContentFragment();
                    frag.setResId(R.id.tab_me);
                    trx.add(R.id.home_content, frag);
                    fragMap.put(R.id.tab_me, frag);
                }else {
                    trx.show(frag);
                }
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + resId);
        }
        trx.commit();
    }

    public void navbarOnClick(View view) {
        showFragment(view.getId());
    }

    // 注意每次点击触发后，需要隐藏其他 fragment 页，否则会重叠
    private void hideAllFragment(FragmentTransaction trx){
        HomeContentFragment frag = fragMap.get(R.id.tab_msg);
        if(frag != null) trx.hide(frag);

        frag = fragMap.get(R.id.tab_friend);
        if(frag != null) trx.hide(frag);

        frag = fragMap.get(R.id.tab_discover);
        if(frag != null) trx.hide(frag);

        frag = fragMap.get(R.id.tab_me);
        if(frag != null) trx.hide(frag);
    }

    @Override
    public boolean handleMessage(@NonNull Message msg) {
        switch (msg.what) {
            case SharedData.UPDATE_MSG_ITEM_MESSAGE:
                ChatMsg chatMsg = (ChatMsg) msg.obj;
                SharedData.msgListAdapter.updateItem(chatMsg);
                break;
        }
        return false;
    }
}
